package main

import (
	"common"
	"common/configx"
	"common/databasex"
	"common/ginx"
	"common/json_response"
	"common/zapx"
	"flag"
	"gateway/internal/controller/v1/book"
	contrComic "gateway/internal/controller/v1/comic"
	contrCommon "gateway/internal/controller/v1/common"
	"gateway/internal/controller/v1/email"
	"gateway/internal/controller/v1/navigate"
	"gateway/internal/controller/v1/swiper"
	"gateway/internal/controller/v1/user"
	"gateway/internal/middleware"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

var configPath = flag.String("config_path", "./config.yaml", "配置文件位置")

func main() {
	flag.Parse()

	if err := common.Init(
		*configPath,
		common.WithBootstrapInit(func(config configx.Config) any {
			return zapx.WithAppName(config.App.Name)
		}),
		common.WithBootstrapInit(func(config configx.Config) any { return zapx.WithDefaultLogger() }),
		common.WithBootstrapInit(func(config configx.Config) any {
			return databasex.WithMysqlDns(config.Mysql.Dns)
		}),
	); err != nil {
		panic(err)
	}

	g := ginx.NewGinX(
		configx.C.App.Debug,
		ginx.WithHttpConfig(&configx.C.Http),
		ginx.WithSentryMiddleware(),
		ginx.WithMiddleware(middleware.CorsHandle()),
		ginx.WithV1RouteGroup(func(group *gin.RouterGroup) {
			eGroup := group.Group("email")
			eGroup.POST("send_sms", email.SendSmsEmail)
		}),
		ginx.WithV1RouteGroup(func(g *gin.RouterGroup) {
			userController := user.NewUserController()
			uGroup := g.Group("user")
			uGroup.POST("register", user.Register)
			uGroup.POST("login", userController.Login)
			uGroup.Use(middleware.CheckAuth(true)).GET("profile", user.GetProfile)
			uGroup.Use(middleware.CheckAuth(true)).POST("edit_profile", user.PostProfile)
		}),
		ginx.WithV1RouteGroup(func(g *gin.RouterGroup) {
			commonController := contrCommon.NewCommonController()
			cGroup := g.Group("common")
			cGroup.POST("upload", commonController.UploadFile)
			cGroup.GET("init", commonController.Init)
		}),
		ginx.WithV1RouteGroup(func(g *gin.RouterGroup) {
			cmGroup := g.Group("comic")
			comicController := contrComic.NewComicController()
			cmGroup.GET("list", comicController.ComicList)
			cmGroup.Use(middleware.CheckAuth(false)).GET("detail/:comic_id", comicController.GetComicDetail)
			cmGroup.GET("chapter_list/:comic_id", comicController.ChapterList)
			cmGroup.Use(middleware.CheckAuth(false)).GET("resource_list/:chapter_id", comicController.ResourceList)
			cmGroup.GET("hot_search", comicController.GetHotSearch)
			cmGroup.Use(middleware.CheckAuth(false)).GET("chapter_detail/:chapter_id", comicController.ChapterDetail)
			cmGroup.Use(middleware.CheckAuth(true)).POST("comic_look_for", comicController.ComicLookFor)
		}),
		ginx.WithV1RouteGroup(func(g *gin.RouterGroup) {
			sGroup := g.Group("swiper")
			sGroup.GET("/", swiper.GetList)
		}),
		ginx.WithV1RouteGroup(func(g *gin.RouterGroup) {
			nGroup := g.Group("navigate")
			nGroup.GET("/", navigate.GetList)
		}),
		ginx.WithV1RouteGroup(func(g *gin.RouterGroup) {
			bookController := book.NewBookController()
			bGroup := g.Group("book")
			bGroup.Use(middleware.CheckAuth(true)).POST("favourite", bookController.Favourite)
			bGroup.Use(middleware.CheckAuth(true)).GET("index", bookController.Index)
		}),
	)

	g.NoRoute(func(context *gin.Context) {
		json_response.NewJsonHttpResponse(
			context,
			json_response.WithMsg("您已进入虚空"),
			json_response.WithCode(json_response.CODE_ERROR),
		).Send()
		return
	})

	g.NoMethod(func(context *gin.Context) {
		json_response.NewJsonHttpResponse(
			context,
			json_response.WithMsg("您的使用姿势不正确"),
			json_response.WithCode(json_response.CODE_ERROR),
		).Send()
		return
	})

	bt := common.NewBootstrap(
		common.WithBootstrapCallFunc(g),
	)
	if err := bt.Boot(); err != nil {
		zapx.Logger.Error("应到程序启动失败", zap.Error(err))
		return
	}

	defer func() {
		bt.Shutdown()
	}()
}
